package com.F06.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.F06.actionForm.F060101ActionForm;
import com.F06.business.F060101Business;
import com.F06.dao.F060101DAO;
import com.action.BaseAction;
import com.config.Constants;
import com.exception.SelectException;
import com.info.T_S_YHCZXXinfo;
import com.util.CommonTools;
import com.util.ExcelExport;
import com.util.FileOperator;
import com.util.StreamUpload;
/**
 * @wangk071226
 * @综合统计下载
 * @功能：生成数据倒入EXCEL
 */
public class F060101Action extends BaseAction {
	/** 记录操作日志 */
	Logger log = Logger.getLogger(F060101Action.class);

	/** 操作记录INFO */
	T_S_YHCZXXinfo t_YhczxxInfo = null;

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		/** 获得actionForm对象 */
		F060101ActionForm actionForm = (F060101ActionForm) form;
		/** 获得operationType值�?�做执行判断�? */
		String operationType = actionForm.getOperationType();
		log.info("operationType = " + operationType);
		/** 获取实例 */
		F060101DAO dao = new F060101DAO();

		try {
			/** 生成操作记录语句 */
			t_YhczxxInfo = setYhczxx(request, response, operationType,
					"D010101");
			/**
			 * @页面初始�?
			 * @统一参数：operationType=init
			 * @规范：左树进入连接页面不执行查询�?
			 */
			if (Constants.INIT.equals(operationType)) {
				this.doINIT(actionForm);
				return mapping.findForward(Constants.List);
			}
			/**
			 * @导出EXCEL文件
			 * @统一参数：operationType=excel
			 * @规范：
			 */
			if (Constants.download.equals(operationType)) {
				this.Download(actionForm, dao, request,response);
				return null;
			}
			/**
			 * @创建数据文件
			 * @统一参数：operationType=createdatefile
			 * @返回boolean值
			 */
			if (Constants.CREATEDATEFILR.equals(operationType)) {
				this.CREATEDATEFILR(actionForm, dao, request,response);
				return mapping.findForward(Constants.List);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (dao != null)
				dao.disConnect();
		}
		return mapping.findForward(Constants.INIT);
	}

	/**
	 * @初始�
	 */
	public void doINIT(F060101ActionForm actionForm) throws SelectException {
		actionForm.setCurrentPage(1);
	}

	/**
	 * @生成数据文件
	 */
	private void CREATEDATEFILR(F060101ActionForm actionForm, F060101DAO dao,
			HttpServletRequest request,HttpServletResponse response) throws SelectException {
		// File.separator 文件分隔符
		String url = request.getSession().getServletContext().getRealPath("/");
		String filename = actionForm.getSignaure_date_year()+"-"+actionForm.getSignaure_date_month()+"产成月报.xls";
		String templeturl = url+"templet"+File.separator+"产成月报模板.xls";
		String buildfileurl = url+"buildfile"+File.separator+filename;
		actionForm.setFilename(filename);
		actionForm.setUrlexcel(buildfileurl);
		log.info("templeturl = "+templeturl);
		log.info("buildfileurl = "+buildfileurl);	
		log.info("filename = "+filename);

		// 写文件到excel文件里,
		F060101Business.writeExcel(templeturl, buildfileurl,dao, actionForm);
		log.info("ERRERFLAG is "+actionForm.getErrorFlag());
	}
	/**
	 * @导出EXCEL文件
	 */
	private void Download(F060101ActionForm actionForm, F060101DAO dao,
			HttpServletRequest request,HttpServletResponse response) throws SelectException {
		String filename = actionForm.getUrlexcel();
		String url = actionForm.getFilename();
		try {
			this.downloadFile(url,filename,response);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			log.info(" 1 ");
			e.getMessage();
			e.printStackTrace();
		}
	}
	/**
	 * 下载指定的文件,如果在Action的execute中调用该方法，则在调用完成以后不能再调用FindForward或者Forwar方法,只能返回一个值为null的ActionForward对象。
	 * 
	 * @param strOriginalFileName
	 *            带完整路径的原始文件名。
	 * @param strNewFileName
	 *            缺省保存在本地的文件名。
	 * @param response
	 *            HTTP服务器的HttpServletResponse对象。
	 * @exception
	 */
	public void downloadFile(String filename,String url, HttpServletResponse response)
			throws IOException {
		File file = new File(url);
		String filelength = String.valueOf(file.length());
		try {
			response.setContentType("application/x-download");
			response.setHeader("Location", filename);
			response.setHeader("Content-Disposition", "attachment;filename=\""
					+CommonTools.toUtf8String(filename));
			response.setContentLength(Integer.parseInt(filelength));
			OutputStream outputStream = response.getOutputStream();
			InputStream inputStream = new FileInputStream(file);
			byte[] buffer = new byte[1024];
			int i = -1;			
			while ((i = inputStream.read(buffer)) != -1) {
				outputStream.write(buffer, 0, i);
			}
			outputStream.flush();
			outputStream.close();
			inputStream.close();
			outputStream = null;

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
		

}
